{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.实现对点宽点宽学园的登录，网站：https://www.digquant.com/account"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.edge.service import Service\n",
    "\n",
    "service = Service(executable_path=r\"E:\\msedgedriver.exe\")\n",
    "driver = webdriver.Edge(service=service)\n",
    "driver.get(\"https://www.digquant.com/account\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这个代码运行结果True代表可以读取到.env，False代表没有读取到\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'DigQuant@01'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# .env文件里面是密码\n",
    "import os\n",
    "os.getenv(\"PASSWORD\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "推荐课程\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import random\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "def simulate_sendkeys(label,str1):\n",
    "    label.clear()\n",
    "    time.sleep(0.5)\n",
    "    for i in str1:\n",
    "        time.sleep(random.random())\n",
    "        label.send_keys(i)\n",
    "    time.sleep(0.5)\n",
    "\n",
    "# 输入账号（电话号码）\n",
    "a=driver.find_element(By.XPATH,\"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[1]/div/div/div/input\")\n",
    "simulate_sendkeys(a,\"15016622681\")\n",
    "\n",
    "# 输入密码\n",
    "b=driver.find_element(By.XPATH,\"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[2]/div/div[1]/div/input\")\n",
    "simulate_sendkeys(b,os.getenv(\"PASSWORD\"))\n",
    "\n",
    "# 模拟鼠标点击“登录”\n",
    "from selenium.webdriver import ActionChains\n",
    "xxx=ActionChains(driver)\n",
    "\n",
    "c=driver.find_element(By.XPATH,\"/html/body/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/form/div[3]\")\n",
    "xxx.click(c).perform()\n",
    "\n",
    "# 等待两秒等到登录进去后\n",
    "time.sleep(2)\n",
    "\n",
    "# 登录进去后随便获取里面的一个文本内容\n",
    "d=driver.find_element(By.XPATH,\"/html/body/div[1]/div[2]/div[1]/div[1]/div[2]/div/div[2]/span\")\n",
    "print(d.text)\n",
    "\n",
    "# 关闭\n",
    "driver.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.爬取到中国金融期货交易所下载当天日行情数据，网站：http://www.cffex.com.cn/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.edge.service import Service\n",
    "\n",
    "driver = webdriver.Edge(service = Service(executable_path=r\"E:\\msedgedriver.exe\"))\n",
    "driver.get(\"http://www.cffex.com.cn/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "所有操作执行完成\n",
      "浏览器已关闭\n"
     ]
    }
   ],
   "source": [
    "# 获取当天的数据\n",
    "from selenium.webdriver import ActionChains\n",
    "from selenium.webdriver.common.by import By\n",
    "import time\n",
    "\n",
    "def click_element(driver, xpath, element_name=\"\"):\n",
    "    \"\"\"点击指定XPATH的元素\"\"\"\n",
    "    try:\n",
    "        element = driver.find_element(By.XPATH, xpath)\n",
    "        action = ActionChains(driver)\n",
    "        action.click(element).perform()\n",
    "        time.sleep(1)\n",
    "        return True\n",
    "    except Exception as e:\n",
    "        print(f\"点击元素失败 {element_name}: {e}\")\n",
    "        return False\n",
    "\n",
    "\n",
    "def main():\n",
    "    try:\n",
    "        # 初始化ActionChains\n",
    "        action_chains = ActionChains(driver)\n",
    "        \n",
    "        # 依次点击各个元素\n",
    "        steps = [\n",
    "            (\"/html/body/div/div[5]/div/ul/li[4]/a\", \"第一个菜单\"),   # “数据”的完整XPath\n",
    "            (\"/html/body/div[5]/div[2]/div[2]/ul/li[2]/a\", \"第二个菜单\"),    # “行情数据”的完整XPath\n",
    "            (\"/html/body/div[5]/div[2]/div[2]/ul/li[2]/ul/li[1]/a\", \"子菜单\"),   # “日统计”的完整XPath\n",
    "            (\"/html/body/div[5]/div[2]/div[3]/div[3]/h1/a\", \"标题链接\")   # 下载附件的完整XPath\n",
    "        ]\n",
    "        \n",
    "        for xpath, name in steps:\n",
    "            if not click_element(driver, xpath, name):\n",
    "                print(f\"在点击 {name} 时失败，退出执行\")\n",
    "                return\n",
    "        \n",
    "        print(\"所有操作执行完成\")\n",
    "        \n",
    "    except Exception as e:\n",
    "        print(f\"程序执行出现异常: {e}\")\n",
    "    finally:\n",
    "        # 关闭浏览器\n",
    "        try:\n",
    "            driver.close()\n",
    "            print(\"浏览器已关闭\")\n",
    "        except Exception as e:\n",
    "            print(f\"关闭浏览器时出错: {e}\")\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
